/*
 * Copyright 2011 group coding kata nantes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.coding.kata.nantes.bowling.christophe;

/**
 * Builder for Frame.
 */
public final class FrameBuilder {

    private static final int RADIX_BASE_10 = 10;

    private FrameBuilder() {
    }

    public static final char STRIKE = 'x';
    public static final char SPARE = '/';
    public static final char MISS = '-';

    public static Frame rolls(final String rolls) {
        String rollsLower = rolls.toLowerCase();
        Frame firstFrame = new Frame();
        rollsToFrame(rollsLower, firstFrame);
        return firstFrame;
    }

    private static void rollsToFrame(final String rollsLower, final Frame frame) {
        if (!rollsLower.isEmpty()) {
            char roll = rollsLower.charAt(0);
            Frame nextFrame;
            if (roll == STRIKE) {
                nextFrame = frame.addStrike();
            } else if (roll == SPARE) {
                nextFrame = frame.addSpare();
            } else if (roll == MISS) {
                nextFrame = frame.addMiss();
            } else {
                nextFrame = frame.addPinDown(Character.digit(roll, RADIX_BASE_10));
            }
            rollsToFrame(rollsLower.substring(1), nextFrame);
        }
    }


}
